<?php
namespace app\index\controller;

use think\facade\Db;
use app\BaseController;
use think\facade\Cache;
use think\Request;

use app\model\User;
//use app\model\User as UserModel;

class Index extends BaseController
{
    protected $middleware = ['appcheck2'];    
    public function index()
    {
            //$data = ['title' => 'title1','writer' => 'writer1','writer1' => 'writer11'];
            $data = ['title' => 'title2','writer' => 'writer3'];
            $data1 =[
                ['title' => 'title1','writer' => 'writer1'],
                ['title' => 'title2','writer' => 'writer1'],
                ['title' => 'title3','writer' => 'writer1'],
                ['title' => 'title4','writer' => 'writer1']
            ] ;

            //$res = Db::name('article')->replace()->insert($data);
            //$res = Db::name('article')->insert($data);
            //$res = Db::name('article')->strict(false)->insertGetId($data);
            $res = Db::name('article')->insertAll($data1);
            print_r($res);

        //return 'ni hao a';
    }

    public function hello(Request $request)
    {
        //$res = Db::table('qing_article')->where('id',3)->find();
        //$res = Db::name('article')->where('id',3)->find();
        //$res = Db::table('qing_article')->where('id',4)->findOrEmpty();
        //$res = Db::table('qing_article')->where('id',4)->findOrFail();
        //$res1 = Db::table('qing_article')->select();
        //foreach($res1 as $k=>$v){
        //    echo $v['title'];
        //}
        //echo $res['title'];
        //halt($res1);
        //查询单个值
        //$res = Db::table('qing_article')->where('id',3)->value('title');
        //查询多列
        $res = Db::table('qing_article')->column('title','id');
        echo '<br/>';
        echo($request->hello);
        halt($res);

    }
    //数据分批处理
    public function hello1()
    {
        $res = Db::table('qing_article')->chunk(10,function($users){
            foreach($users as $user){
                //echo $user['title'].'<br/>';
                if($user['title']=='title1'){
                    echo $user['title'];
                }
            }
        },'id','desc');
    }
    //游标查询，适合大量数据
    public function hello2()
    {
        $res1 = Db::table('qing_article')->cursor();
        foreach($res1 as $k=>$v){
            echo $v['title'].'<br/>';
        }
    }
    //更新数据
    public function hello3()
    {
        $data = ['id'=>3,'title'=>'name','writer'=>'wenfei1'];
        $res = Db::table('qing_article')->update($data);
        halt($res);
    }
    //删除数据
    public function hello4()
    {
        
        $res = Db::table('qing_article')->delete(1);
        //$res = Db::table('qing_article')->delete(1,2,3);
        //$res = Db::table('qing_article')->where('id',1)->delete();
        //$res = Db::table('qing_article')->where('id','<',3)->delete();
        halt($res);
    }
    //软删除数据
    public function hello5()
    {
        $res = Db::table('qing_article')->where('id',4)->useSoftDelete('time',time())->delete();
        halt($res);
    }
    //查询表达式
    public function hello6()
    {
        //$res = Db::table('qing_article')->where('id','=',4)->select();
        //id不等于4
        //$res = Db::table('qing_article')->where('id','<>',4)->select();
        //$res = Db::table('qing_article')->where('id','<=',14)->select();
        //$res = Db::table('qing_article')->where('title','like','nam%')->select();
        //$res = Db::table('qing_article')->where('title','like',['%jing','php%'],'OR')->select();
        //$res = Db::table('qing_article')->whereLike('title','%jing')->select();
        //$res = Db::table('qing_article')->where('id','between',[1,8])->select();
        //$res = Db::table('qing_article')->whereIn('id',[1,5,8])->select();
        //$res = Db::table('qing_article')->whereNotIn('id','1,5,8')->select();
        $res = Db::table('qing_article')->whereNull('time')->select();


        halt($res);
    }

    //查询表达式链式操作
    public function hello7()
    {
        $res = Db::table('qing_article')
        ->order('id desc')
        ->limit(10)
        ->where('id','<',20)
        ->select();

        halt($res);
    }
     //查询表达式链式操作
     public function hello8()
     {
         $article=Db::table('qing_article');
         $res = $article->order('id desc')
         ->limit(10)
         ->where('id','<',20)
         ->select();
 
         halt($res);
     }

     //查询表达式链式操作 多表查询
     public function hello9()
     {
         $res=Db::name('article')->alias('a')->field('a.title,b.name')
         ->join('user b','a.user_id=b.id')->select();
 
         halt($res);
     }
     //查询表达式链式操作 多表查询
     public function hello10()
     {
         $res=Db::name('article')->count();
 
         halt($res);
     }
     //查询表达式链式操作 多表查询
     public function hello11()
     {
         //$res=Db::name('user')->whereTime('delete_time','<','2020-10-01')->select();
         $res=Db::name('user')->whereBetweenTime('delete_time','2019-10-01','2021-04-01')->select();
         halt($res);
     }
     //查询表达式链式操作 多表查询
     public function hello12()
     {
         //$res=Db::name('user')->whereTime('delete_time','<','2020-10-01')->select();
         $res=Db::name('user')->whereBetweenTime('delete_time','2019-10-01','2021-04-01')->fetchSql(true)->select();
         halt($res);
     }

     //闭合查询
     public function hello13()
     {
         $res=Db::table('qing_article')->where('user_id','IN',function($query){
            $query->table('qing_user')->where('id','>',1)->field('id');
         })->select();
         halt($res);
     }
     //原生查询
     public function hello14()
     {
         $res=Db::query('select * from qing_user where id=2');
         halt($res);
     }
     //事务操作
     public function hello15()
     {
         $res=Db::transaction(function(){
            Db::table('qing_user')->delete('id',1);
            Db::table('qing_article')
            ->insert(['title'=>'你好','user_id'=>'aaa']);    
         })->select();
         halt($res);
     }

     //model 查询
     public function getUser()
     {
         $user = new User();
         $res=$user::select();
         halt($res);
     }
     //model 查询并转成对象数组
     public function getUsers()
     {
         $users=User::select()->toArray();
         halt($users); 
     }
     //model 查询单条数据并转成对象数组
     public function getUserObjectToArray()
     {
         $user=User::find(20);
         echo $user; 
     }

     //model 查询
     public function getUser1()
     {
         $user = User::find(1);
         echo $user->delete_time;
         echo $user->name;
     }
     //model 查询
     public function getUser2()
     {
        $user = new User();
        $user->getUser();
     }
     //model 条件查询
     public function getUser3()
     {
        $user = User::where('name','wenfei441033403')->find();
        echo $user->name;
     }
        //model 查询
        public function getUser4()
        {
           $user = User::findOrEmpty(100);
           echo $user;
        }    
        //model 查询
        public function getUser5()
        {
           $user = User::find(100);
           halt($user);
        }  
    //model 多条查询
    public function getUser6()
    {
       $list = User::order('id','desc')->select([12,13,14]);
       foreach($list as $key=>$user){
        echo $user->name.'<br/>';
       }
    }       
     //model 新增
     public function saveUser()
     {
        $user = new User();
        $user->name='wenfei'.mt_rand();
        $user->number=mt_rand();
        echo $user->save();
     }
     //model 新增
     public function saveUser1()
     {
        $user = new User();
        echo $user->save([
            'name'=>'wenfei'.mt_rand(),
            'number'=>mt_rand(),
        ]);
     }
     //model 新增
     public function saveUser2()
     {
        $user = new User();
        $data = ['name'=>'wenfei'.mt_rand(),'number'=>mt_rand()];
        $user->allowField(['name','number'])->save($data);
        echo $user->id;
     }
     //model 新增多条数据
     public function saveUser3()
     {
        $user = new User();
        $list = [['name'=>'wenpan'.mt_rand(),'number'=>mt_rand()], 
        ['name'=>'wenbo'.mt_rand(),'number'=>mt_rand()]];
        $user->allowField(['name','number'])->saveAll($list);
        //echo $user->id;
     }
     //model 更新单条数据
     public function updateUser()
     {
        //$user = User::find(1);
        $user = User::where('name','wenfei216516065')->find();
        $user->name='wenjing'.mt_rand();
        $user->number=mt_rand();
        echo $user->save();
     }
      //model 更新单条数据
      public function updateUser1()
      {
         //$user = User::find(1);
         $user = User::update(['id'=>1,'name'=>'wengui'.mt_rand(),'number'=>mt_rand()]);
      }
          //model 批量更新单条数据
      public function updateUser2()
          {
            $user = new User();
            $list = [['id'=>1,'name'=>'wenpan'.mt_rand(),'number'=>mt_rand()], 
            ['id'=>2,'name'=>'wenbo'.mt_rand(),'number'=>mt_rand()]];
            $user->allowField(['name','number'])->saveAll($list);
            //echo $user->save();
          }
      //model 更新单条数据
     public function updateUser3()
     {
        $user = User::find(100);
        //$user->name='wenjing'.mt_rand();
        //$user->number=mt_rand();
        //echo $user->save();
        echo $user;
     }    
     //model 删除单条数据
     public function deleteUser()
     {
        $user = User::find(1);
        echo $user->delete();
     }    
     //model 根据主键删除数据
     public function deleteUser1()
     {
        //根据主键删除单条数据
        //User::destroy(2);
        //根据主键删除多条数据
        User::destroy([3,4,5]);
     }    
      //model 条件删除数据
      public function deleteUser2()
      {
         User::destroy(function($query){
             $query->where('id','<',10);
         });
      }    
      
}
